package com.ferrlo.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.onlineframework.commons.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ferrlo.model.Admin;
import com.ferrlo.service.IAdminManager;

@Controller
public class AdminLoginController {

	@Resource(name = "adminManager")
	private IAdminManager adminManager;

	@RequestMapping(value = "/adminLogin")
	public String login(HttpServletRequest request) {

		String username = request.getParameter("username");
		String pwd = MD5Util.getMD5(request.getParameter("pwd"));
		
		Admin admin = findAdminByAName(username);
		if (admin != null) {
			if (pwd.equals(admin.getAPwd())) {
				return "../admin/project-publish";
			}
		}

		return "../admin/adminLogin";
	}

	private Admin findAdminByAName(String name) {
		String dqQuery = "AdminManager.findAdminByAName.query";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		
		List<Admin> admins = adminManager.findListByDynamicQuery(dqQuery, map);
		if (admins.size() != 0) {
			return admins.get(0);
		}
		return null;
	}

}
